<template>
  <div class="login-wrapper">
    <canvas ref="artCanvas" class="art-canvas"></canvas>
    <div class="login-container">
      <div class="form-group">
        <label for="username">用户名</label>
        <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入用户名"
            @keyup.enter="handleLogin"
        />
      </div>
      <div class="form-group">
        <label for="password">密码</label>
        <input
            type="password"
            id="password"
            v-model="password"
            placeholder="请输入密码"
            @keyup.enter="handleLogin"
        />
      </div>
      <button class="login-btn" @click="handleLogin">登录</button>
      <button class="register-link-btn" @click="goToRegister">注册新用户</button>
      <p v-if="errorMessage" class="error-message">{{ errorMessage }}</p>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
// const instance = axios.create({
//   baseURL: 'http://localhost:8080', // 后端地址
//   withCredentials: true // 允许发送Cookie
// });
//
// export default instance;
export default {
  name: 'Login',
  data() {
    return {
      username: '',
      password: '',
      errorMessage: ''
    };
  },
  mounted() {
    const canvas = this.$refs.artCanvas;
    const ctx = canvas.getContext('2d');

    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight;

    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';

    ctx.font = 'bold 80px "Arial", sans-serif';
    ctx.fillStyle = 'rgb(240,244,247)';
    ctx.fillText('招财蛙', canvas.width / 2, canvas.height / 2 - 180);

/*    ctx.font = 'italic 60px "Arial", sans-serif';
    ctx.fillStyle = 'rgba(102, 187, 106, 0.3)';
    ctx.fillText('Joblab', 1.8 * canvas.width / 3, 1.8 * canvas.height / 2 - 150);*/
  },


/*  // 定义登录方法
  const handleLogin = ()=>{
    console.log(user.value);
//   前端通过axios给后端发送请求，请求登录
    axios.post(BASE_URL+'/v1/user/login',user.value)
        .then((response)=>{
          // .then()回调函数，用于处理后端返回的响应数据
          console.log(response.data);
          if(response.data.code==2000){
            ElMessage.success('登录成功');
            //localStorage存储的是字符串类型的键值对 <key,value>
            //console.log(response.data.data);//是JsonResult对象中data属性的值,也就是后端返回的userVO
            //console.log(JSON.stringify(response.data.data));//将对象转成JSON格式的字符串
            localStorage.setItem('user',JSON.stringify(response.data.data));
            router.push('/');
          }else{
            ElMessage.error((response.data.msg));
            user.value={username: '',password: ''}
          }

        })
  }*/





  methods: {
    async handleLogin() {
      if (!this.username || !this.password) {
        this.errorMessage = '用户名和密码不能为空';
        return;
      }
      this.errorMessage = '';

      try {
        const response = await this.$axios.post('http://localhost:9190/v1/user/login', {
          username: this.username,
          password: this.password
        },{ withCredentials: true });

        console.log('登录结果:', response.data);

        if (response.data.state===300){
          alert(response.data.data);
        }

        if (response.data.state===200){
          localStorage.setItem('token', response.data.data.token); // 存储Token
          // localStorage.setItem('user',JSON.stringify(response.data.data));
          console.log(response.data.data)
          this.$router.push('/');
        }

      } catch (error) {
        console.error('登录失败:', error);
        if (error.response && error.response.data) {
          this.errorMessage = error.response.data.message || '登录失败';
        } else {
          this.errorMessage = '无法连接到服务器';
        }
      }
    },
    goToRegister() {
      this.$router.push('/register');
    }
  }
};
</script>

<style scoped>
.art-canvas {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 0;
}

.login-container {
  position: relative;
  z-index: 1;
}

.login-wrapper {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
/*  background-color: #3D6BFC;*/
  background-image: linear-gradient(to bottom right, #2A4ABF, #3D6BFC); /* 渐变背景 */
  position: relative;
  overflow: hidden;
  background-size: cover; /* 让背景图覆盖整个容器 */
  background-position: center; /* 背景图居中显示 */
  background-repeat: no-repeat; /* 背景图不重复 */
}

.login-wrapper::before,
.login-wrapper::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  //background-color: #a3d9a3;
  border-radius: 50%;
  opacity: 0.5;
}

.login-wrapper::before {
  top: 20%;
  left: 10%;
}

.login-wrapper::after {
  bottom: 15%;
  right: 15%;
}

h2 {
  margin-bottom: 25px;
  color: #2e7d32;
  text-align: center;
}

.form-group {
  margin-bottom: 20px;
  text-align: left;
}

label {
  display: block;
  margin-bottom: 5px;
  font-weight: bold;
  color: #f0f4f7;
}

input {
  width: 100%;
  padding: 10px;
  border: 1px solid #a3d9a3;
  border-radius: 5px;
  box-sizing: border-box;
  background-color: #fff;
  transition: border-color 0.3s;
}

input:focus {
  border-color: #66bb6a;
  outline: none;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background-color: #0f4873;
  color: #fff;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
  transition: background-color 0.3s;
}

.login-btn:hover {
  background-color: #81c784;
}

.register-link-btn {
  width: 100%;
  padding: 8px;
  margin-top: 10px;
  background-color: transparent;
  color: #f0f4f7;
  border: 1px solid #a3d9a3;
  border-radius: 5px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.3s;
}

.register-link-btn:hover {
  background-color: #a3d9a3;
  color: #fff;
}

.error-message {
  color: #d32f2f;
  margin-top: 15px;
  text-align: center;
}
</style>
